package com.denlaku.longan.controller.impl;

import com.denlaku.longan.BizModule;
import com.denlaku.longan.controller.DashboardViewController;
import com.denlaku.longan.core.Current;
import com.denlaku.longan.core.Terminal;
import com.denlaku.longan.http.Resp;
import com.denlaku.longan.http.Return;
import com.denlaku.longan.qo.DashboardQuery;
import com.denlaku.longan.qo.MyCollectQuery;
import com.denlaku.longan.qo.Query;
import com.denlaku.longan.qo.RefreshQuery;
import com.denlaku.longan.service.DashboardIndexService;
import com.denlaku.longan.service.DashboardService;
import com.denlaku.longan.service.MyCollectService;
import com.denlaku.longan.service.RefreshService;
import com.denlaku.longan.util.Lists;
import com.denlaku.longan.util.Objects;
import com.denlaku.longan.vo.ComponentData;
import com.denlaku.longan.vo.Dashboard;
import com.denlaku.longan.vo.PageBy;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @author tianx
 */
@RestController
public class DashboardViewControllerImpl implements DashboardViewController {
    @Autowired
    private DashboardService dashboardService;

    @Autowired
    private DashboardIndexService dashboardIndexService;

    @Autowired
    private MyCollectService myCollectService;

    @Autowired
    private RefreshService refreshService;

    @Override
    public Return<Dashboard> get(@RequestParam("id") Long id) {
        Dashboard dashboard = dashboardService.get(Query.of(id));
        MyCollectQuery query = new MyCollectQuery();
        query.setDataId(id);
        query.setBiz(BizModule.DASHBOARD);
        query.setUserId(Current.getUserId());
        Objects.ifPresent(dashboard, () -> query.setTerminal(dashboard.getTerminal()));
        Boolean collected = myCollectService.collected(query);
        return Resp.success(dashboard).setExt(collected);
    }

    @Override
    public Return<List<Dashboard>> myList(@RequestBody DashboardQuery dashboard, PageBy pageBy) {
        PageHelper.startPage(pageBy.getPage(), pageBy.getSize());
        List<Dashboard> list = dashboardService.list(dashboard);
        return Resp.success(list);
    }

    @Override
    public Return<List<ComponentData>> refreshes(@RequestBody List<RefreshQuery> queries,
            @RequestParam(defaultValue = "false") boolean debug) {
        List<ComponentData> dataList = refreshService.refreshes(queries, debug);
        return Resp.success(Lists.filter(dataList, Objects::nonNull));
    }

    @Override
    public Return<List<Map<String, String>>> search(@RequestParam("keyword") String keyword) {
        if (StringUtils.isEmpty(keyword)) {
            return Resp.success(Lists.empty());
        }
        List<Map<String, String>> result = dashboardIndexService.search(keyword, Terminal.pc);
        return Resp.success(result);
    }

}
